package com.czkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/")
    public Object index() {
        //不带条件查询
        return sysUserService.list();
    }
    @RequestMapping("/getPageList2")
    public Object getPageList2(Integer pageIndex,Integer pageSize) {
        IPage<SysUser> page=new Page<SysUser>(pageIndex,pageSize);
        return sysUserService.getList();
    }

    @RequestMapping("/getPageList")
    public Object getPageList(Integer pageIndex,Integer pageSize) {
        IPage<SysUser> page=new Page<SysUser>(pageIndex,pageSize);
        return sysUserService.page(page);
    }



    @RequestMapping("/getList")
    public Object getListByParams(SysUser sysUser) {

        //条件构造器
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>()
//                .eq(sysUser.getUsrName() != null, "usrName", sysUser.getUsrName())
//                .like("usrName", sysUser.getUsrName())
                ;
//        //精确匹配 =
//        queryWrapper.eq(sysUser.getUsrName() != null, "usrName", sysUser.getUsrName());
//        //模糊匹配  以xx开始   以XX结尾   包含XX
//        queryWrapper.like("usrName", sysUser.getUsrName());
//
//        //范围查询 > <  >=  <=
////        queryWrapper.gt()
////        queryWrapper.lt()
////        queryWrapper.ge()
////        queryWrapper.le()
//        queryWrapper.between("usrId", 1, 3);

        //
        queryWrapper.lambda()
                .like(SysUser::getUsrName, sysUser.getUsrName())
        ;
        return sysUserService.list(queryWrapper);
    }


    @RequestMapping("/save")
    public Object save(SysUserVo sysUser) {
        boolean result = false;

        if (sysUser.getUsrId() != 0) {
            result = sysUserService.updateById(sysUser);
        } else {
            result = sysUserService.save(sysUser);
        }

        return result;
    }
}
